package com.JobFind.logic.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Component;

import net.sf.json.JSONArray;

import com.JobFind.DAO.IRecruitmentDAO;
import com.JobFind.DTO.RecruitmentBriefDTO;
import com.JobFind.DTO.RecruitmentDTO;
import com.JobFind.DTO.RecruitmentEasyDTO;
import com.JobFind.DTO.RecruitmentSimplyDTO;
import com.JobFind.logic.IRecruitmentLogic;
import com.JobFind.model.TbRecruitment;

@Component
public class RecruitmentLogic implements IRecruitmentLogic{
	private IRecruitmentDAO recruitmentDAO;
	
	
	public IRecruitmentDAO getRecruitmentDAO() {
		return recruitmentDAO;
	}

	@Resource
	public void setRecruitmentDAO(IRecruitmentDAO recruitmentDAO) {
		this.recruitmentDAO = recruitmentDAO;
	}


	@Override
	@Transactional
	public String getJobList(Integer userId,String sQuery) throws Exception {
		String result = "";
		List<TbRecruitment> recruBean=this.recruitmentDAO.getJobList(userId,sQuery);
		List<RecruitmentSimplyDTO> recruitmentSimplyDTO = new ArrayList<RecruitmentSimplyDTO>();
		try {
			for(TbRecruitment recruitment:recruBean){
				recruitmentSimplyDTO.add(recruitment.toSimplyDTO());
			}
			result = JSONArray.fromObject(recruitmentSimplyDTO).toString();
		} catch (Exception e) {
		  e.printStackTrace();
		  result  ="";
		}
		return result;
	}

	@Override
	@Transactional
	public String getRecruitmentInfoById(Integer recruitmentId)
			throws Exception {
		String result = "";
		TbRecruitment recruitmentBean = null;
		RecruitmentEasyDTO recruitmentEasyDTO = new RecruitmentEasyDTO();
		try {
			recruitmentBean = this.recruitmentDAO.getRecruitmentInfoById(recruitmentId);
			recruitmentEasyDTO = recruitmentBean.toEasyDTO();
			result = JSONArray.fromObject(recruitmentEasyDTO).toString();
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}

	@Override
	@Transactional
	public String getRecruitByCompanyId(Integer companyId) throws Exception {
		String result = "";
		List<TbRecruitment> recruitmentBean = null;
		List<RecruitmentBriefDTO> recruitmentBriefListDTO = new ArrayList<RecruitmentBriefDTO>();
		try {
			recruitmentBean = this.recruitmentDAO.getRecruitInfoByCompanyId(companyId);
			for(TbRecruitment recruitment: recruitmentBean){
				recruitmentBriefListDTO.add(recruitment.toBriefDTO());
			}
			result = JSONArray.fromObject(recruitmentBriefListDTO).toString();
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}

	@Override
	@Transactional
	public Integer updateSomeRecritInfo(Integer recruitmentId,
			String recruitmentPosition, String recruitmentInfo,
			String recruitmentRemarks) throws Exception {
		Integer result = 0;
		try {
			result = this.recruitmentDAO.updateSomeRecritInfo(recruitmentId, recruitmentPosition, recruitmentInfo, recruitmentRemarks);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer deleJob(Integer recruitmentId) throws Exception {
		Integer result = 0;
		try {
			result = this.recruitmentDAO.deleJob(recruitmentId);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer addRecritmentInfo(RecruitmentDTO recruitmentDTO)
			throws Exception {
		Integer result = 0;
		try {
			result = this.recruitmentDAO.addRecruitmnet(recruitmentDTO.toDAO());
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer upRecritmentAreNum(Integer recruitmentId) throws Exception {
		Integer result = 0;
		try {
			result = this.recruitmentDAO.upRecritmentAreNum(recruitmentId);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

}
